import 'package:flutter/material.dart';
import 'package:xinxuan/common/address_bar.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:get/get.dart';
import 'package:xinxuan/common/goods_order_info_item.dart';
import 'package:xinxuan/common/network_image_util.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/common/store_order_btns.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/controllers/store_order_controller/store_order_detail_page_controller.dart';
import 'package:xinxuan/controllers/store_order_controller/store_order_page_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:xinxuan/utils/color_util.dart';

class StoreOrderDetailPage extends StatelessWidget {

  final StoreOrderPageController storeOrderPageController = Get.find();
  final CommonPageController commonPageController = Get.find();

  @override
  Widget build(BuildContext context) {
    Map<String, dynamic> argumentsMap = Get.arguments;
    String title = argumentsMap['title'];
    int orderId = argumentsMap['orderId'];
    return GetBuilder<StoreOrderDetailPageController>(
      init: StoreOrderDetailPageController(),
      builder: (controller) {
        return Scaffold(
          backgroundColor: pageBgColor,
          appBar: AppBarUtil(
            title: title,
            backgroundColor: pageBgColor,
          ),
          body: controller.orderDetailData.value.id! <= 0 ? Container() : ScrollConfigurationCommon(child: ListView(
            children: [
              AddressBar(
                consignee: controller.orderDetailData.value.address!.consignee ?? '', 
                mobile: controller.orderDetailData.value.address!.mobile ?? '', 
                provinceStr: controller.orderDetailData.value.address!.provinceStr ?? '', 
                cityStr: controller.orderDetailData.value.address!.cityStr ?? '', 
                districtStr: controller.orderDetailData.value.address!.districtStr ?? '', 
                address: controller.orderDetailData.value.address!.address ?? '', 
                id: 1,
                showRight: false,
              ),
              SizedBox(height: 12.h),
              Column(
                children: controller.orderDetailData.value.products!.map((productItem) {
                  return _goodsInfo(
                    productItem.title ?? '', 
                    productItem.image ?? '', 
                    productItem.price ?? '', 
                    productItem.quantity ?? 0, 
                    productItem.specStr ?? '',
                    isStar: controller.orderDetailData.value.isStar ?? 0,
                    gratitude: controller.orderDetailData.value.gratitude ?? ''
                  );
                }).toList(),
              ),
              SizedBox(height: 12.h),
              Container(
                padding: EdgeInsets.symmetric(vertical: 8.h),
                color: bgWhiteColor,
                child: Column(
                  children: [
                    GoodsOrderInfoItem(title: '订单编号', value: '${controller.orderDetailData.value.orderSn}', showCopy: true),
                    GoodsOrderInfoItem(title: '创建时间', value: '${controller.orderDetailData.value.createdAt}'),
                  ],
                ),
              ),
              SizedBox(height: 12.h),
              Container(
                padding: EdgeInsets.symmetric(vertical: 8.h),
                color: bgWhiteColor,
                child: Column(
                  children: [
                    GoodsOrderInfoItem(title: '商品总价', value: '¥${controller.orderDetailData.value.totalAmount}'),
                    // GoodsOrderInfoItem(title: '购物金抵扣', value: '-¥0.00'),
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
                      alignment: Alignment.centerRight,
                      child: Text.rich(
                        TextSpan(
                          text: '应付款:  ',
                          style: TextStyle(
                            color: textBlackColor,
                            fontSize: 12.sp,
                            fontWeight: FontWeight.bold
                          ),
                          children: [
                            TextSpan(
                              text: '¥',
                              style: TextStyle(
                                color: mainTextColor,
                                fontSize: 12.sp,
                                fontWeight: FontWeight.bold
                              )
                            ),
                            TextSpan(
                              text: '${controller.orderDetailData.value.totalAmount}',
                              style: TextStyle(
                                color: mainTextColor,
                                fontSize: 16.sp,
                                fontWeight: FontWeight.bold
                              )
                            )
                          ]
                        )
                      ),
                    )
                  ],
                ),
              ),
              SizedBox(height: 33.h),
              Container(
                padding: EdgeInsets.symmetric(horizontal: 25.w),
                child: Container(
                  child: ButtonUtil(
                    text: '在线客服', 
                    fontSize: 14.sp, 
                    borderRadius: 18.w, 
                    height: 36.h, 
                    onTop: () {
                      commonPageController.openWeChatCustomerServiceChat();
                    }
                  ),
                ),
              )
            ],
          )),
          bottomNavigationBar: controller.orderDetailData.value.status == 1 || controller.orderDetailData.value.status == 5 ? BottomAppBar(
            elevation: 0,
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 8.w),
              height: 56.h,
              child: StoreOrderBtns(
                status: controller.orderDetailData.value.status!, 
                toPayFun: () async {
                  var res = await Get.toNamed(
                    '${AppRouters.COMMON_PAGE}${AppRouters.GOOD_DETAIL_PAGE}${AppRouters.STORE_CONFIRM_ORDER_PAGE}${AppRouters.STORE_ORDER_PAY_PAGE}',
                    arguments: {
                      'orderId': orderId,
                      'isDetail': false
                    }
                  );
                  if (res != null && res) {
                    controller.getStoreOrderDetail(orderId);
                    storeOrderPageController.getStoreTabItemOrderList(storeOrderPageController.tabIndex.value);
                  }
                }, 
                cleanOrderFun: () async {
                  await storeOrderPageController.storeOrderCancel(orderId);
                  controller.getStoreOrderDetail(orderId);
                }, 
                receiveOrderFun: () async {
                  await storeOrderPageController.storeOrderReceive(orderId);
                  controller.getStoreOrderDetail(orderId, loading: false);
                }
              ),
            ),
          ) : null,
        );
      },
    );
  }

  /// 商品信息
  Widget _goodsInfo(String title, String headImage, String price, int quantity, String specStr, { int isStar = 0, String gratitude = ''}) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
      color: bgWhiteColor,
      child: Row(
        children: [
          NetworkImageUtil(
            url: headImage, 
            width: 90.w, 
            height: 90.w,
            borderRadius: 5.w,
          ),
          SizedBox(width: 12.w),
          Expanded(child: Container(
            constraints: BoxConstraints(
              minHeight: 90.w
            ),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Expanded(child: Container(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        title,
                        overflow: TextOverflow.ellipsis,
                        maxLines: 2,
                        style: TextStyle(
                          color: textBlackColor,
                          fontSize: 14.sp
                        ),
                      ),
                      SizedBox(height: 6.h),
                      Text(
                        specStr,
                        style: TextStyle(
                          color: textSubtitleColor,
                          fontSize: 12.sp
                        ),
                      ),
                      SizedBox(height: 6.h),
                      isStar == 1 ? Text(
                        '感恩值:$gratitude',
                        style: TextStyle(
                          color: mainTextColor,
                          fontSize: 14.sp,
                          fontWeight: FontWeight.bold
                        ),
                      ) : Container()
                    ],
                  ),
                )),
                SizedBox(width: 12.w),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Text(
                      '¥$price',
                      style: TextStyle(
                        color: textBlackColor,
                        fontSize: 12.sp
                      )
                    ),
                    Text(
                      'x$quantity',
                      style: TextStyle(
                        color: textSubtitleColor,
                        fontSize: 12.sp
                      ),
                    )
                  ],
                ),
              ],
            )
          ))
        ],
      ),
    );
  }
}